<template>
	<view>
		<h5 style="margin-top:20rpx;font-size:50rpx;text-align: center;">{{ getTips }}</h5>
		<!-- <img :src="imgData" ref="img" class="img-data" /> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: {},
				state: 0,
				imgData: '',
				pusher: null,
				scanWin: null,
				snapshotTimeoutNumber: 2000,
				faceInitTimeout: null,
				snapshTimeout: null
			};
		},
		onLoad(data) {
			this.data = data
			uni.showToast({
				title: "正在打开摄像头，请稍后",
				icon: "none"
			})
			//#ifdef APP-PLUS
			this.faceInit();
			//#endif
			const eventChannel = this.getOpenerEventChannel();
			this.eventChannel = eventChannel;
		},
		onHide() {
			this.faceInitTimeout && clearTimeout(this.faceInitTimeout);
			this.snapshTimeout && clearTimeout(this.snapshTimeout);
			// this.scanWin.hide();
		},
		computed: {
			getTips() {
				const tips = ['请正对摄像头', '正在识别人脸...', '未识别到人脸', '登录成功！', '登录失败，请先注册']
				return tips[this.state]
			}
		},
		methods: {
			// （5）将base64图片【this.imgData】发送给后台
			faceHttp() {
				this.state = 1
				const image = this.imgData.split(',')[1]
				const data = this.data.method === 'search' ? {
					image,
					FaceMatchThreshold: this.data.score
				} : {
					image,
					PersonName: this.data.name
				}
				uni.request({
					url: `http://192.168.2.26:3000/tencent/${this.data.method}`,
					method: 'post',
					data,
					dataType: 'json',
					success: res => {
						if (res.data.status && res.data.status === 412) {
							this.state = 2
							setTimeout(() => {
								this.snapshotPusher()
							}, 1000)
						} else {
							console.log('res.data', res.data)
							if (this.data.method === 'search') {
								this.state = res.data.Results[0].Candidates.length === 0 ? 4 : 3
								this.eventChannel.emit('refreshopts', this.getTips)
								uni.navigateBack()
								console.log('res.data', res)
							} else {
								if (res.data.SimilarPersonId !== '') {
									uni.showModal({
										content: '已经存在，无须再注册',
										showCancel: false,
										success: res => {
											if (res.confirm) {
												uni.navigateBack()
											}
										}
									})
								}else{
									uni.showModal({
										content: '注册成功！',
										showCancel: false,
										success: res => {
											if (res.confirm) {
												uni.navigateBack()
											}
										}
									})
								}
							}
							return

						}
					},
					fail: error => {
						this.state = 2
						console.log('error', error)
						setTimeout(() => {
							this.snapshotPusher()
						}, 1000)
					}
				})
			},
			// （1）通过plus.video.createLivePusher创建直播推流
			pusherInit() {
				//获取当前窗口对象
				const currentWebview = this.$mp.page.$getAppWebview();
				//创建视频推流对象，url是视频上传到的服务器地址，不填表示不传视频
				this.pusher = plus.video.createLivePusher('livepusher', {
					url: '',
					top: '20%',
					left: '10%',
					width: '80%',
					height: '60%',
					position: 'absolute',
					aspect: '9:16',
					'z-index': 999
				});
				// 将推流对象append到当前页面中
				currentWebview.append(this.pusher);
				this.pusher.preview();
				this.pusher.switchCamera()
			},

			faceInit() {
				this.faceInitTimeout = setTimeout(() => {
					this.pusherInit();
					//覆盖在视频之上的内容，-比如扫描框 
					// （2）利用plus.webview.create将扫描框页面及扫描动画（xxx.html）覆盖在视频之上；
					// this.scanWin = plus.webview.create('/hybrid/html/scan.html', '', {
					// 	background: 'transparent'
					// });
					// // 显示窗口
					// this.scanWin.show();
					this.snapshotPusher();
					uni.hideToast();
				}, 200);
			},
			// （3）利用liverPusher对象的snapshot方法创建视频快照
			snapshotPusher() {
				this.state = 0
				this.snapshTimeout = setTimeout(() => {
					this.pusher.snapshot(
						e => {
							// this.pusher.close();
							// this.scanWin.hide();
							var src = e.tempImagePath;
							// 使用plus.zip.compressImage压缩图片
							this.getMinImage(src);
						},
						function(e) {
							plus.nativeUI.alert('snapshot error: ' + JSON.stringify(e));
						}
					);
				}, this.snapshotTimeoutNumber);
			},
			// （4）使用plus.zip.compressImage压缩图片
			getMinImage(imgPath) {
				plus.zip.compressImage({
						src: imgPath,
						dst: imgPath,
						overwrite: true,
						quality: 40
					},
					zipRes => {
						setTimeout(() => {
							var reader = new plus.io.FileReader();
							reader.onloadend = res => {
								var speech = res.target.result; //base64图片
								console.log(speech.length);
								this.imgData = speech;
								//将图片发送给后台
								this.faceHttp();
							};
							//一定要使用plus.io.convertLocalFileSystemURL将target地址转换为本地文件地址，否则readAsDataURL会找不到文件
							reader.readAsDataURL(plus.io.convertLocalFileSystemURL(zipRes.target));
						}, 1000);
					},
					function(error) {
						console.log('Compress error!', error);
					}
				);
			},

		},

	};
</script>

<style lang="scss" scoped>
	.img-data {
		width: 100%;
		height: auto;
	}
</style>
